package com.luoyx.hauyne.admin.sys.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.luoyx.hauyne.admin.amqp.producer.UserProfileUpdatedProducer;
import com.luoyx.hauyne.admin.api.sys.UserAPI;
import com.luoyx.hauyne.admin.api.sys.dto.UserDTO;
import com.luoyx.hauyne.admin.api.sys.query.LoginLookupQuery;
import com.luoyx.hauyne.admin.api.sys.request.UserProfileUpdatedMsg;
import com.luoyx.hauyne.admin.sys.entity.UserProfile;
import com.luoyx.hauyne.admin.sys.mapper.UserProfileMapper;
import com.luoyx.hauyne.admin.sys.query.UserQuery;
import com.luoyx.hauyne.admin.sys.request.ModifyPasswordDTO;
import com.luoyx.hauyne.admin.sys.request.UserCreateDTO;
import com.luoyx.hauyne.admin.sys.response.CreatedUserVO;
import com.luoyx.hauyne.admin.sys.response.UserAutoCompleteVO;
import com.luoyx.hauyne.admin.sys.response.UserDetailVO;
import com.luoyx.hauyne.admin.sys.response.UserPageResultVO;
import com.luoyx.hauyne.admin.sys.service.UserService;
import com.luoyx.hauyne.mybatisplus.dto.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import java.net.URI;
import java.util.List;

import static com.luoyx.hauyne.admin.api.sys.UserAPI.SYS_USERS;

@Slf4j
@Api(tags = "用户管理")
@RestController
@RequestMapping(value = SYS_USERS)
@RequiredArgsConstructor
@Validated
public class UserController implements UserAPI {

    private final UserService userService;
    private final UserProfileUpdatedProducer userProfileUpdatedProducer;
    private final UserProfileMapper userProfileMapper;

    /**
     * 按用户名查找
     *
     * @param loginLookupQuery 登录查询条件
     * @return 用户DTO对象
     */
    @ApiOperation(value = "按用户名查询用户信息")
    @GetMapping(value = "/login-lookup")
    @Override
    public UserDTO loginLookup(LoginLookupQuery loginLookupQuery) {
        return userService.loginLookup(loginLookupQuery);
    }

    @ApiOperation(value = "分页查询用户信息")
    @GetMapping
    public PageResult<UserPageResultVO> findPage(@Validated UserQuery userQuery) {
        return userService.findPage(userQuery);
    }

    /**
     * 新增用户
     *
     * @param userCreateDTO 表单参数
     * @return 新增的用户
     */
    @ApiOperation(value = "新增用户")
    @PostMapping
    public ResponseEntity<CreatedUserVO> create(@Validated @RequestBody UserCreateDTO userCreateDTO) {
        CreatedUserVO createdUserVO = userService.create(userCreateDTO);
        return ResponseEntity.created(URI.create(SYS_USERS + "/" + createdUserVO.getId())).body(createdUserVO);
    }

    @ApiOperation(value = "按主键查询用户详情")
    @GetMapping(value = "/{id}")
    public UserDetailVO detail(@ApiParam(value = "用户id") @PathVariable(value = "id") Long id) {
//        return userService.detail(id);
        return null;
    }

    /**
     * 删除用户
     *
     * @param id 用户ID
     */
    @ApiOperation(value = "删除用户")
    @ResponseStatus(value = HttpStatus.NO_CONTENT)
    @DeleteMapping(value = "/{id}")
    public void delete(@ApiParam(value = "用户ID", required = true) @PathVariable(value = "id") Long id) {
        userService.removeById(id);
    }

    /**
     * 修改密码
     *
     * @param modifyPasswordDTO 表单参数
     * @throws Exception
     */
    @ApiOperation(value = "修改密码")
    @PatchMapping(value = "/modify-password")
    public void modifyPassword(@Validated @RequestBody ModifyPasswordDTO modifyPasswordDTO) throws Exception {
        userService.modifyPassword(modifyPasswordDTO);
    }

    /**
     * 用户自动完成接口（按用户名、真实姓名、昵称、手机号、电子邮箱地址模糊查询）
     *
     * @param keyword 关键词
     * @return
     */
    @ApiOperation(value = "用户自动完成接口（按用户名、真实姓名、昵称、手机号、电子邮箱地址模糊查询）")
    @ApiImplicitParam(name = "keyword", value = "关键词（用户名、真实姓名、昵称、手机号、电子邮箱地址）", required = true)
    @GetMapping(value = "/suggestions")
    public List<UserAutoCompleteVO> suggestions(@RequestParam(value = "keyword") String keyword) {
        return userService.findSuggestions(keyword);
    }


    @ApiOperation(value = "更新用户姓名")
    @PostMapping(value = "/update-user-full-name")
    public void updateUserFullName(@RequestBody String userFullName) {
        userProfileMapper.update(null,
                Wrappers.<UserProfile>lambdaUpdate()
                        .set(UserProfile::getRealName, userFullName)
                        .eq(UserProfile::getId, 1)
        );
        UserProfileUpdatedMsg userProfileUpdatedMsg = new UserProfileUpdatedMsg();
        userProfileUpdatedMsg.setUserId(1L);
        userProfileUpdatedMsg.setFullName(userFullName);

        userProfileUpdatedProducer.send(userProfileUpdatedMsg);
    }
}
